//
// Created by 33145 on 2023/6/1.
//

#ifndef ROCKET_IO_THREAD_H
#define ROCKET_IO_THREAD_H

#include <pthread.h>
#include <semaphore.h>
#include <memory>
#include "eventloop.h"

namespace rocket {
  class IOThread {
  public:
    typedef std::shared_ptr<IOThread> ptr;
    IOThread();
    ~IOThread();

    EventLoop::ptr getEventLoop();

    void start();  // 线程创建好后会进入阻塞，等待start调用后启动
    void join();

    static void* mainFunc(void* arg);

  private:
    pid_t _thread_id {-1};
    pthread_t _thread {0};
    EventLoop::ptr _eventloop {nullptr};  // 一个线程一个eventloop

    sem_t _init_semaphore{};   // 等待线程初始化完毕
    sem_t _start_semaphore{};  // 负责线程开始
  };
}

#endif //ROCKET_IO_THREAD_H
